<?php
class Weather {
	
	public static function parseWeather() {
		set_time_limit(0);
		
		$arrCities = array(
			//1 => 6002,
// 			2 => 5998,
// 			3 => 5995,
// 			4 => 5993,
// 			5 => 6618,
// 			6 => 6617,
// 			7 => 241819,
// 			8 => 6607,
// 			9 => 6035,
// 			10 => 6025,
// 			11 => 6038,
// 			12 => 6024,
// 			13 => 6042,
// 			14 => 29482,
// 			15 => 1948,
// 			16 => 1950,
// 			17 => 6468,
			//18 => 14179
				19 => 3673,
				20 => 3673,
				21 => 71994,
				22 => 3626,
				23 => 3678,
				24 => 3635,
				25 => 5698,
				26 => 5777,
				28 => 3740,
				29 => 3763,
				30 => 3778,
				31 => 3785,
				32 => 3787,
				33 => 3719
				
		);
		
		$arrYears = array(2012, 2013);
		$objDB = Service_Database::instance();
		$objCurl = new Service_Curl();
		$objDOM = new Service_SimpleHtmlDom();
		
		foreach ($arrCities as $resortId => $weatherId) {
				
			foreach ($arrYears as $year) {
		
				for ($month = 1; $month <= 12; $month++) {
					$result = file_get_contents('http://www.gismeteo.ru/diary/' . $weatherId . '/' . $year . '/' . $month . '/');
				//	$this->verbouseOutput($cityId . ', ' . $year . '-' . $month);
					$objDOM->load($result);
					$arrTrs = $objDOM->find('table tr');
					if (empty($arrTrs)) continue(2);
					foreach ($arrTrs as $objTr) {
						$arrInsert = array();
						$i = 0;
						$arrTds = $objTr->find('td');
						foreach ($arrTds as $objTd) {
							switch (++$i) {
								case 1:
									$day = intval($objTd->text());
									$arrInsert['date'] = $year . '-' . ($month > 9 ? $month : '0' . $month) . '-' . ($day > 9 ? $day : '0' . $day);
									break;
								case 2:
									$arrInsert['daily_temp'] = intval($objTd->text());
									break;
								case 3:
									$arrInsert['daily_pressure'] = intval($objTd->text());
									break;
								case 4:
									$cloudHtml = $objTd->innerText();
									$cloud = null;
									if (strpos($cloudHtml, 'suncl.png')) $cloud = 'suncl';
									elseif (strpos($cloudHtml, 'sunc.png')) $cloud = 'sunc';
									elseif (strpos($cloudHtml, 'sun.png')) $cloud = 'sun';
									elseif (strpos($cloudHtml, 'dull.png')) $cloud = 'dull';
									$arrInsert['daily_cloud'] = $cloud;
									break;
								case 5:
									$eventHtml = $objTd->innerText();
									$event = null;
									if (strpos($eventHtml, 'snow')) $event = 'snow';
									elseif (strpos($eventHtml, 'rain')) $event = 'rain';
									elseif (strpos($eventHtml, 'storm')) $event = 'storm';
									$arrInsert['daily_event'] = $event;
									break;
								case 6:
									$arrWind = explode(' ', $objTd->text());
									$arrInsert['daily_wind'] = intval(@$arrWind[1]);
									break;
								case 7:
									$arrInsert['night_temp'] = intval($objTd->text());
									break;
								case 8:
									$arrInsert['night_pressure'] = intval($objTd->text());
									break;
								case 9:
									$cloudHtml = $objTd->innerText();
									$cloud = null;
									if (strpos($cloudHtml, 'suncl.png')) $cloud = 'suncl';
									elseif (strpos($cloudHtml, 'sunc.png')) $cloud = 'sunc';
									elseif (strpos($cloudHtml, 'sun.png')) $cloud = 'sun';
									elseif (strpos($cloudHtml, 'dull.png')) $cloud = 'dull';
									$arrInsert['night_cloud'] = $cloud;
									break;
								case 10:
									$eventHtml = $objTd->innerText();
									$event = null;
									if (strpos($eventHtml, 'snow')) $event = 'snow';
									elseif (strpos($eventHtml, 'rain')) $event = 'rain';
									elseif (strpos($eventHtml, 'storm')) $event = 'storm';
									$arrInsert['night_event'] = $event;
									break;
								case 11:
									$arrWind = explode(' ', $objTd->text());
									$arrInsert['night_wind'] = intval(@$arrWind[1]);
									break;
							}
						}
						if (!empty($arrInsert)) {
							$arrInsert['ID_resort'] = $resortId;
							$objDB->insert('weather', $arrInsert, 'update');
						}
					}
					sleep(1);
				}
			}
		}
	}
	
}